package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingIssueRecovery;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.bean.JyDrawingTrain;
import com.jy.adscm.zltx.service.JyDrawingListService;
import com.jy.adscm.zltx.service.JyDrawingTrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 图纸修订的培训记录 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2018-11-08
 */
@RestController
@RequestMapping("/adscm/zltx/jyDrawingTrain")
public class JyDrawingTrainController {

    @Autowired
    JyDrawingTrainService jyDrawingTrainService;
    @Autowired
    JyUserService jyUserService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
   @Autowired
   JyDrawingListService jyDrawingListService;
    /**
     * 查询培训记录
     * @param drawingId
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/selectDrawingTrain/{drawingId}")
    public SimpleCoreResponse selectDrawingTrain(@PathVariable Integer drawingId) throws AdscmSystemException {

        Wrapper<JyDrawingTrain> wrapper = new EntityWrapper<>();
        wrapper.eq("drawing_id",drawingId);
        List<JyDrawingTrain> trains = jyDrawingTrainService.selectList(wrapper);
        if (trains != null && trains.size()>0){
            for (JyDrawingTrain train : trains) {
                Integer host = train.getTrainHost();
                if (host != null){
                    JyUser user = jyUserService.selectById(host);
                    train.setTrainHostName(user.getName());
                }
            }
        }
        return new SimpleCoreResponse("200","查询成功" ,trains);
    }

    /**
     * 修改培训记录
     * @param train
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/update")
    public SimpleCoreResponse update(@RequestBody JyDrawingTrain train, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        JyUser user = token.getUser();

        train.setTrainHost(user.getId());
        jyDrawingListService.updateJyDrawing(Integer.valueOf(String.valueOf(train.getDrawingId())));
        jyDrawingTrainService.updateById(train);

        return new SimpleCoreResponse("200","修改成功" ,null);
    }

}

